<%@ page import="java.util.*,java.sql.*, org.json.simple.*;" %>
	<%!
		//-------------------------------------------Block Field
		private String url="jdbc:mysql://localhost/hrd_center";
		private String user="root";
		private String pwd="";
		private Connection con;
		private Statement stmt;
		private ResultSet rs;
		private String word,status,className,stu_id;
		private int id;
		//------------------------------------------Start Block Method
		//Function get data 
		public ResultSet getData(String query){
			try{
				 stmt=con.createStatement();// Creating command to execute sql			
				 rs=stmt.executeQuery(query);// store data in resultset
				
			}catch(SQLException e){}
				 return rs;
		}
		//Function Update Status
		public boolean updateStatus(){
			String sql="Update hrd_students Set stu_status=1-stu_status Where stu_id='"+stu_id+"'";
				try{
					 stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);// Creating command to execute sql			
					 stmt.executeUpdate(sql);// // Update Image
				}catch(SQLException e){
					return false;
					}
			    return true;	
		}
		//------------------------------------------End Block Method
	%>
	<%
		word=request.getParameter("name").trim();// Getting value name
	    className=request.getParameter("class").trim();// Getting value class name
	    status=request.getParameter("status").trim(); // Getting value status
	    id=Integer.parseInt(request.getParameter("id"));
	    stu_id=request.getParameter("stu_id");//Getting value student id
	    if(className.equals("All Class")){
			 className="";
		 }if(status.equals("All Status")){
			 status="";
		 }
		try{
			String query="Select * From hrd_students Where stu_name like '%"+word+"%'"+
						 "and stu_class like '%"+className+"%'"+
						 "and stu_status like '%"+status+"%' ";
			 Class.forName("com.mysql.jdbc.Driver");// Loading driver
			 con=DriverManager.getConnection(url,user,pwd); // Establishing connection
			 if(updateStatus()){// checking if updating process
				 getData(query);// get all data and store temporary on table 
			 }else{
				 getData(query);// get all data and store temporary on table  
			 } 
				out.print("[");
			while(rs.next()){ // Checking if have data
				JSONObject array=new JSONObject(); // creating object json
		 		ResultSetMetaData data=rs.getMetaData(); // get all info about table, columnname,labelname,columntotal.......		
		 		for(int i=1;i<data.getColumnCount()+1;i++){
		 			array.put(data.getColumnName(i), rs.getString(i));	// add data to json object
		 	  	   }
		 		if(!rs.isLast())
		 	   		out.println(array.toJSONString()+",");// return json object to string 
		 		else{
		 			out.println(array.toJSONString());
		 		}
			}
				out.println("]");
					
			 stmt.close();// Closing command
			 rs.close();// Closing result set
			 con.close();// Closing connection
		}catch(ClassNotFoundException e){
			out.print("Error loading driver,check again");
		}catch(SQLException e){
			out.println("Error establishing database");
		}
	%>